﻿using AutoUI;
using MESPro;
using MESPro.Controls;
using MESPro.Models;
using MESPro.Models.FunctionType;
using SqlSugar;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Layout;

namespace RM.Controls
{
    [RMControl("TabControl容器", "网站应用小17.ico")]
    public partial class RMTabcontrol : TabControl//, ICustomControl
    {
        #region Public Constructors

        public RMTabcontrol()
        {
            InitializeComponent();
        }

        #endregion Public Constructors

        #region 字段

        //}
        private bool a = false;

        //private void RMTabcontrol_SelectedIndexChanged(object sender, EventArgs e)
        //{
        private int h = 250;

        #endregion 字段

        #region Protected 方法

        protected override void OnPaint(PaintEventArgs pe)
        {
           
            base.OnPaint(pe);
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
        }

        #endregion Protected 方法

        //private void RMTabcontrol_Selected(object sender, TabControlEventArgs e)
        //{
        //}

        //private void RMTabcontrol_Selecting(object sender, TabControlCancelEventArgs e)
        //{

        #region Private 方法
        public bool Isdesigner { get; set; }
        //}
        private void RMTabcontrol_MouseClick(object sender, MouseEventArgs e)
        {
            if (!Isdesigner) return;
            a = !a;
            if (a)
            {
                this.Height = this.ItemSize.Height;
            }
            else
            {
                this.Height = h;
            }
        }

        private void RMTabcontrol_DragEnter(object sender, DragEventArgs e)
        {
            var data = (e.Data.GetData(typeof(ListViewItem))) as ListViewItem;
            if (data != null)
            {
                var ty = data.Tag as Type;
                if (ty != typeof(RMTabcontrol))
                {
                    e.Effect = DragDropEffects.Move;
                }
                else
                {
                    //  App.Show("控件只能放在RMTabcontrol容器中");
                }
            }
        }

        /// <summary>
        /// ufo 设计时添加子控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RMTabcontrol_DragDrop(object sender, DragEventArgs e)
        {
            var data = (e.Data.GetData(typeof(ListViewItem))) as ListViewItem;
            var point = this.PointToClient(new Point(e.X, e.Y));
            if (data != null)
            {
                var ty = data.Tag as Type;
                var tab = (RMTabcontrol)sender;
                var page = tab.SelectedTab;
                var pages = ((RMTabcontrol)sender).Controls;
                //  var form = ((RMTabcontrol)sender).FindForm() as Designer;

                #region 利用反射生成控件,还没有测试对性能的影响

                var rMButton = (Control)ty.Assembly.CreateInstance(ty.FullName);
                //rMButton.Size=new Size(20, 20);
                rMButton.Text = ty.BaseType.Name;
                // RMButton rMButton = new RMButton() { Text = "按钮" };
                rMButton.Location = point;

                page.Controls.Add(rMButton);
                rMButton.AddMove();
                rMButton.Click +=(s,e)=> { MainServerFrm.RMControlClick?.Invoke(s); };

                #endregion 利用反射生成控件,还没有测试对性能的影响

                return;

                #region 实例化RMButton

                if (ty == typeof(RMButton))
                {
                }

                #endregion 实例化RMButton

                #region 实例化Pdf

                if (ty == typeof(RMPdf))
                {
                    RMPdf btn = new RMPdf();
                    btn.Location = point;
                    foreach (TabPage item in pages)
                    {
                        item.Controls.Add(btn);
                        // btn.AddMove();
                        btn.MouseDown += (sender2, e2) =>
                        {
                            if (e2.Button == MouseButtons.Right)
                            {
                                ; ;// btn.Location = point;
                            }
                            ;
                        };
                        btn.KeyUp += (s, ee) =>
                        {
                            if (ee.KeyData == Keys.Delete || ee.KeyData == Keys.Back)
                            {
                                var ss22 = s as Control;
                                ss22.Parent.Controls.Remove(btn);
                            }
                        };
                        btn.Click += RMButton_Click;
                    }
                }

                #endregion 实例化Pdf
            }
            ;
        }

        private void RMButton_Click(object sender, EventArgs e)
        {
            //var form = this.FindForm() as Designer;
            //form.RMControl_Click?.Invoke(sender);
        }
        #region 属性

        [Category("\t\t\t\t\t\t\t\t参数设置"), Description("很重要,用于数据绑定,关联存储过程的参数"), DisplayName("1.控件名"), Browsable(true)]
        public new string Name { get => base.Name; set => base.Name = value; }

        [Category("\t\t\t\t\t\t\t\t参数设置"), Description("显示名称"), DisplayName("2.显示名称"), Browsable(true)]
        public new string Text
        {
            get => base.Text;
            set
            {
                base.Text = value;
                if (App.User is not null)
                {
                    DisLanguage.SetLanguage(App.User.Language, value);
                }
            }
        }

        [Category("\t\t\t\t\t\t\t\t参数设置"),
     Description("点击这个按钮会执行的存储过程"),
     DisplayName("\t\t\t\t存储过程"),
     TypeConverter(typeof(ExpandableObjectConverter)),
     Editor(typeof(PropertyReturnStoreProcedure), typeof(UITypeEditor))]
        [SugarColumn(IsJson = true)]
        public StoreProInfo StoredProcedure { get; set; } = new();

        [TypeConverter(typeof(ExpandableObjectConverter))]
        [Category("\t\t\t\t\t\t\t\t参数设置"), Description("多语言设置"), DisplayName("多语言设置"), Browsable(true)]
        public DisLanguage DisLanguage { get; set; } = new DisLanguage() { zh_CN = "按钮", en_US = "Button" };

        [DisplayName("固定位置"), Description("固定位置"), Browsable(false)]
        public override AnchorStyles Anchor { get => base.Anchor; set => base.Anchor = value; }

        [DisplayName("定位"), Description("停靠"), Browsable(true)]
        public new Point Location { get => base.Location; set => base.Location = value; }

        [DisplayName("大小"), Description("控件的大小"), Browsable(true)]
        public new Size Size { get => base.Size; set => base.Size = value; }

        [DisplayName("停靠位置"), Description("停靠位置"), Browsable(true)]
        public override DockStyle Dock { get => base.Dock; set => base.Dock = value; }

        [DisplayName("字体设置"), Description("字体设置"), Browsable(false)]
        public override Font Font { get => base.Font; set => base.Font = value; }

        [DisplayName("背景色"), Description("背景色"), Browsable(true)]
        public override Color BackColor { get => base.BackColor; set => base.BackColor = value; }

        [DisplayName("字体颜色"), Description("字体颜色"), Browsable(true)]
        public override Color ForeColor { get => base.ForeColor; set => base.ForeColor = value; }

        [Browsable(false)]
        public new Cursor Cursor { get => base.Cursor; set => base.Cursor = value; }

        [Browsable(false)]
        public new bool Visible { get => base.Visible; set => base.Visible = value; }

        [Browsable(false)]
        public new AccessibleRole AccessibleRole { get => base.AccessibleRole; set => base.AccessibleRole = value; }

        [Browsable(false)]
        public new string AccessibleName { get => base.AccessibleName; set => base.AccessibleName = value; }

        [Browsable(false)]
        public new string AccessibleDescription { get => base.AccessibleDescription; set => base.AccessibleDescription = value; }

        [Browsable(false)]
        public new bool TabStop { get => base.TabStop; set => base.TabStop = value; }

        [Browsable(false)]
        public new int TabIndex { get => base.TabIndex; set => base.TabIndex = value; }

        [Browsable(false)]
        public new bool Enabled { get => base.Enabled; set => base.Enabled = value; }

        [Browsable(false)]
        public new bool UseWaitCursor { get => base.UseWaitCursor; set => base.UseWaitCursor = value; }

        [Browsable(false)]
        public override Image BackgroundImage { get => base.BackgroundImage; set => base.BackgroundImage = value; }

        [Browsable(false)]
        public override RightToLeft RightToLeft { get => base.RightToLeft; set => base.RightToLeft = value; }

        [Browsable(false)]
        public override LayoutEngine LayoutEngine => base.LayoutEngine;

        [Browsable(false)]
        public new bool CausesValidation { get => base.CausesValidation; set => base.CausesValidation = value; }

        [Browsable(false)]
        public override bool AutoSize { get => base.AutoSize; set => base.AutoSize = value; }

        [Browsable(false)]
        public new Padding Margin { get => base.Margin; set => base.Margin = value; }

   

        [Browsable(false)]
        public new Size MaximumSize { get => base.MaximumSize; set => base.MaximumSize = value; }

        [Browsable(false)]
        public new Size MinimumSize { get => base.MinimumSize; set => base.MinimumSize = value; }

        [Browsable(false)]
        public override bool AllowDrop { get => base.AllowDrop; set => base.AllowDrop = value; }

        [Browsable(false)]
        public override Point AutoScrollOffset { get => base.AutoScrollOffset; set => base.AutoScrollOffset = value; }

        [Browsable(false)]
        public override BindingContext BindingContext { get => base.BindingContext; set => base.BindingContext = value; }

        //[Browsable(false)]
        //protected override bool CanEnableIme => base.CanEnableIme;
        //[Browsable(false)]
        //public override ContextMenu ContextMenu { get => base.ContextMenu; set => base.ContextMenu = value; }
        [Browsable(false)]
        public override ImageLayout BackgroundImageLayout { get => base.BackgroundImageLayout; set => base.BackgroundImageLayout = value; }

        [Browsable(false)]
        public override ContextMenuStrip ContextMenuStrip { get => base.ContextMenuStrip; set => base.ContextMenuStrip = value; }

        //[Browsable(false)]
        //public override Cursor Cursor { get => base.Cursor; set => base.Cursor = value; }
        [Browsable(false)]
        public new ControlBindingsCollection DataBindings { get => base.DataBindings; }

        [Browsable(false)]
        public new object Tag { get => base.Tag; set => base.Tag = value; }

        [Browsable(false)]
        protected override ImeMode ImeModeBase { get => base.ImeModeBase; set => base.ImeModeBase = value; }

        [Browsable(false)]
        protected override bool CanRaiseEvents => base.CanRaiseEvents;

        #endregion 属性
        #endregion Private 方法
    }
}